/*
 * logicports.c
 *
 *  Created on: Sep 13, 2012
 *      Author: jacco
 */

#include "logicports.h"

/*NOT GATE*/
int not_gate (int a)
{
	int out = LOGIC_X;

	if(a == LOGIC_0 )
	{
		out = LOGIC_1;
	}
	else if ( a == LOGIC_1 )
	{
		out = LOGIC_0;
	}
	else
	{
		out = LOGIC_X;
	}
	return out;
}

/*OR GATE*/
int or_gate (int a, int b)
{
	int out = LOGIC_X;

	if(a == LOGIC_0 && b == LOGIC_0)
	{
		out = LOGIC_0;
	}
	else if(a == LOGIC_1 || b == LOGIC_1)
	{
		out = LOGIC_1;
	}
	else
	{
		out = LOGIC_X;
	}
	return out;
}

/*NOR GATE*/
int nor_gate (int a, int b)
{
	int out = LOGIC_X;

	out = not_gate(or_gate(a,b));

	return out;
}

/*AND GATE*/
int and_gate (int a, int b)
{
	int out = LOGIC_X;

	if (a == LOGIC_0 || b == LOGIC_0)
	{
		out = LOGIC_0;
	}
	else if (a == LOGIC_1 && b == LOGIC_1)
	{
		out = LOGIC_1;
	}
	else
	{
		out = LOGIC_X;
	}
	return out;
}

/*NAND GATE*/
int nand_gate (int a, int b)
{
	int out = LOGIC_X;

	out = not_gate(and_gate(a,b));

	return out;
}

/*XOR GATE*/
int xor_gate (int a, int b)
{
	int out = LOGIC_X;

	if ((a == LOGIC_0 && b == LOGIC_0)||(a == LOGIC_1 && b == LOGIC_1))
	{
		out = LOGIC_0;
	}
	else if (a == LOGIC_X || b == LOGIC_X)
	{
		out = LOGIC_X;
	}
	else
	{
		out = LOGIC_1;
	}
	return out;
}

/*XNOR GATE*/
int xnor_gate (int a, int b)
{
	int out = LOGIC_X;

	out = not_gate(xor_gate(a,b));

	return out;
}


